#include "stdafx.h"
#include "PrintThread.h"

namespace AirCrash {
namespace Calypso {

PrintThread::PrintThread() {
}

PrintThread::~PrintThread() {
}

void PrintThread::run() {
	QPainter painter;
	painter.begin(mPrinter);
	
	mX = 0;
	mY = 0;

	//Set printer
	mPageMargins = Config::get("Print", "margin").toRect();
	mRectSize = Config::get("Print", "size").toSize();
	
	mNumColls = mPageMargins.width() / mRectSize.width(),
	mNumRows = mPageMargins.height() / mRectSize.height();

	int res(mPrinter->resolution());

	painter.setFont( QFont("arial", Config::get("Print", "fontSize").toInt()) );

	mPageMargins = mmToPixel(mPageMargins, res);
	mRectSize = mmToPixel(mRectSize, res);

	int widthData(mRectSize.width() * mNumColls);
	mPageMargins.setX(mPageMargins.x() + ((mPageMargins.width() - widthData) / 2));

	switch (mPrintType) {
		case PrintType::Codes : 
			//Get the codes
			mQuery.exec("SELECT Id FROM Codes WHERE Day = 1");
			printCodes("Vrijdag", painter);
			mQuery.exec("SELECT Id FROM Codes WHERE Day = 2");
			printCodes("Zaterdag", painter);
			mQuery.exec("SELECT Id FROM Codes WHERE Day = 3");
			printCodes("Zondag", painter);
			break;
		case PrintType::List :
			break;
		case PrintType::Items :
			printItems(painter);
			break;
		default:
			QMessageBox::warning(NULL, "ERROR", "Geen type geselecteerd");
	}

	painter.end();
}

void PrintThread::printCodes(QString day, QPainter& painter) {
	mX = 0;
	if (mQuery.first()) {
		QRect drawRect(mPageMargins.left() + (mX * mRectSize.width()), mPageMargins.top() + (mY * mRectSize.height()), 
			mRectSize.width() * mNumColls, mRectSize.height() * 2);
		painter.drawText(drawRect, Qt::AlignCenter, day);
		mY += 2;
		do {
			drawRect = QRect(mPageMargins.left() + (mX * mRectSize.width()), mPageMargins.top() + (mY * mRectSize.height()), 
				mRectSize.width(), mRectSize.height());

			painter.drawRect(drawRect);
			painter.drawText(drawRect, Qt::AlignCenter, mQuery.value(0).toString());

			++mX;
			if (mX == mNumColls) {
				mX = 0;
				++mY;
				if (mY == mNumRows) {
					mY = 0;
					mPrinter->newPage();
				}
			}
		} while (mQuery.next());
		if (mX != 0 && day != "Zondag") {
			++mY;
			if (mY + 2 >= mNumRows) {
				mY = 0;
				mPrinter->newPage();
			}
		}
	}
}

void PrintThread::printItems(QPainter& painter) {
	mQuery.exec("SELECT Id, Summary, Jackpot, Friday, Saturday, Sunday FROM Items");
	mX = 0;
	QRect drawRect;
	int width(mPageMargins.width() / 9);

	drawRect = QRect(mPageMargins.left() + (mX * width), mPageMargins.top() + (mY * mRectSize.height()), 
				width, mRectSize.height());
	painter.drawText(drawRect, Qt::AlignCenter, "Nr");
	drawRect.moveTo(drawRect.x() + width, drawRect.y());
	drawRect.setWidth(drawRect.width() * 4);
	painter.drawText(drawRect, Qt::AlignCenter, "Omschrijving");
	drawRect.setWidth(drawRect.width() / 4);
	drawRect.moveTo(drawRect.x() + width * 4, drawRect.y());
	painter.drawText(drawRect, Qt::AlignCenter, "Hp");
	drawRect.moveTo(drawRect.x() + width, drawRect.y());
	painter.drawText(drawRect, Qt::AlignCenter, "Vrijdag");
	drawRect.moveTo(drawRect.x() + width, drawRect.y());
	painter.drawText(drawRect, Qt::AlignCenter, "Zaterdag");
	drawRect.moveTo(drawRect.x() + width, drawRect.y());
	painter.drawText(drawRect, Qt::AlignCenter, "Zondag");

	drawRect = QRect(mPageMargins.left(), mPageMargins.top(), mPageMargins.width(), mRectSize.height());
	painter.drawLine(drawRect.bottomLeft(), drawRect.bottomRight());

	++mY;

	if (mQuery.first()) {
		do {
			drawRect = QRect(mPageMargins.left() + (mX * width), mPageMargins.top() + (mY * mRectSize.height()), 
				width, mRectSize.height());

			painter.drawText(drawRect, Qt::AlignCenter, mQuery.value(0).toString());

			drawRect.moveTo(drawRect.x() + width, drawRect.y());
			drawRect.setWidth(drawRect.width() * 4);
			painter.drawText(drawRect, Qt::AlignCenter, mQuery.value(1).toString());
			drawRect.setWidth(drawRect.width() / 4);

			drawRect.moveTo(drawRect.x() + width * 4, drawRect.y());
			painter.drawText(drawRect, Qt::AlignCenter, QString(mQuery.value(2).toBool() ? "Ja" : "Nee"));

			drawRect.moveTo(drawRect.x() + width, drawRect.y());
			painter.drawText(drawRect, Qt::AlignCenter, mQuery.value(3).toString());

			drawRect.moveTo(drawRect.x() + width, drawRect.y());
			painter.drawText(drawRect, Qt::AlignCenter, mQuery.value(4).toString());

			drawRect.moveTo(drawRect.x() + width, drawRect.y());
			painter.drawText(drawRect, Qt::AlignCenter, mQuery.value(5).toString());

			++mY;
			if (mY == mNumRows) {
				mY = 0;
				mPrinter->newPage();
			}
		} while (mQuery.next());
	}

	drawRect = QRect(mPageMargins.left(), mPageMargins.top() + (mY * mRectSize.height()), 
			mPageMargins.width(), mRectSize.height());
	painter.drawLine(drawRect.topLeft(), drawRect.topRight());
	++mY;

	mQuery.exec("SELECT SUM(Friday), SUM(Saturday), SUM(Sunday) FROM Items");
	if (mQuery.first()) {
		if (mY + 4 >= mNumRows) {
			mY = 0;
			mPrinter->newPage();
		}
		mX = 2;
		drawRect = QRect(mPageMargins.left() + (mX * width), mPageMargins.top() + (mY * mRectSize.height()), 
			mPageMargins.width() - (mX * width), mRectSize.height());

		painter.drawText(drawRect, Qt::AlignLeft | Qt::AlignVCenter, "Totaal Vrijdag: " + mQuery.value(0).toString());
		drawRect.moveTo(drawRect.x(), drawRect.y() + mRectSize.height());
		painter.drawText(drawRect, Qt::AlignLeft | Qt::AlignVCenter, "Totaal Zaterdag: " + mQuery.value(1).toString());
		drawRect.moveTo(drawRect.x(), drawRect.y() + mRectSize.height());
		painter.drawText(drawRect, Qt::AlignLeft | Qt::AlignVCenter, "Totaal Zondag: " + mQuery.value(2).toString());
		drawRect.moveTo(drawRect.x(), drawRect.y() + mRectSize.height());
		int total(mQuery.value(0).toInt() + mQuery.value(1).toInt() + mQuery.value(2).toInt());
		painter.drawText(drawRect, Qt::AlignLeft | Qt::AlignVCenter, "Totaal Weekend: " + QString::number(total));
	}
}

void PrintThread::setPrinter(QPrinter * printer) {
	mPrinter = printer;
}

void PrintThread::setPrintType(PrintType::Enum type) {
	mPrintType = type;
}

}
}
